
(provide 'org-include-img-from-zip)

(add-hook 'org-mode-hook #'modi/org-include-img-from-zip)
;(add-hook 'find-file-hook #'modi/org-include-img-from-zip)

(defun modi/org-include-img-from-zip (&rest ignore)
  "Convert the zip files to image files.

Only looks at #HEADER: lines that have \":convertfromzip t\".
This function does nothing if not in org-mode, so you can safely
add it to `before-save-hook'."
  (interactive)
  (when (derived-mode-p 'org-mode)
    (save-excursion
      (goto-char (point-min))
      (while (search-forward-regexp
              ;"^\\s-*#\\+HEADER:.*\\s-:convertfromzip\\s-+t"
			  "^\\s-*#\\+STARTUP:.*\\s-convertfromzip"
              nil 'noerror)
        (let* (filenoext imgext imgfile imgfilename zipfile rimgpath cmd)
		  (setq zipfile "arc.zip")
          ;; Keep on going on to the next line till it finds a line with
          ;; `[[FILE]]'
          (while (progn
                   (forward-line 1)
                   (not (looking-at "\\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]"))))
          (when (looking-at "\\[\\[file:\\(.*\\)\\.\\(.*\\)\\]\\]")
            (setq filenoext (match-string-no-properties 1))
            (setq imgext (match-string-no-properties 2))
			(setq rimgpath (concat filenoext "." imgext))
            (setq imgfile (expand-file-name (concat filenoext "." imgext)))
			(setq imgfilename (file-name-nondirectory imgfile))
			;(setq bufpath buffer-file-name)
			;(setq bufname (file-name-base bufpath))
			;(message "%s" (substring rimgpath 0 1));rimgpath)
			(when (string= (substring rimgpath 0 1) "~")
			  (setq rimgpath (concat "%HOME%" (substring rimgpath 1 nil)))
			  (setq rimgpath (file-name-directory rimgpath))
			  ; (message "%s" rimgpath)
			  ;(when (eq (file-exists-p imgfile) nil)
				(setq cmd (concat "7z e " zipfile " -y -o"rimgpath " " imgfilename " " imgfilename ))
				;(message "%s" cmd)
				(with-temp-buffer (shell-command cmd t))
				;(when (file-newer-than-file-p zipfile imgfile)
				 ;; This block is executed only if zipfile is newer than imgfile
				;; or if imgfile does not exist
				;; Source: https://www.gnu.org/software/emacs/manual/html_node/elisp/Testing-Accessibility.html
				; (message "%s" cmd)
				; (shell-command cmd))
			  )
			))))))

(add-hook 'org-open-link-functions 'org-pass-link-to-system)
(defun org-pass-link-to-system (link)
  (if (string-match "^[a-zA-Z0-9]+:" link)
	(message "org-pass-link-to-system")
     (browse-url link)
    nil)
  )

;;http://stackoverflow.com/questions/28697108/can-emacs-show-an-image-in-a-zip-archieve-file-in-org-mode;
;;by kaushalmodi: http://stackoverflow.com/users/1219634/kaushalmodi;
;(add-hook 'find-file-hook #'modi/org-include-img-from-archive)
;; Execute the `modi/org-include-img-from-archive' function just before saving the file
;(add-hook 'before-save-hook #'modi/org-include-img-from-archive)
;; Execute the `modi/org-include-img-from-archive' function before processing the
;; file for export
;(add-hook 'org-export-before-processing-hook #'modi/org-include-img-from-archive)

(defun modi/org-include-img-from-archive (&rest ignore)
  "Extract image files from the archive files. Only .zip files are supported
as of now.

Only looks at #HEADER: lines that have \":extractfromarchive t\".
This function does nothing if not in org-mode, so you can safely
add it to `before-save-hook'."
  (interactive)
  (when (derived-mode-p 'org-mode)
    (save-excursion
      (goto-char (point-min)) ; go to the beginning of the buffer
      (while (search-forward-regexp
              "^\\s-*#\\+HEADER:.*\\s-:extractfromarchive\\s-+t"
              nil :noerror)
        (let (;; only .zip supported as of now
              (search-expr "\\[\\[\\(.*?\\)_zip/\\(.*?\\)\\([^/]+\\..*\\)\\]\\]")
              arc-file
              path-in-arc-file
              img-file img-file-full-path
              dest-dir dest-dir-full-path
              cmd)
          ;; Keep on going on to the next line till it finds a line with
          ;; `[[./path/to/zip-file/path/inside/zip/to/the/image]]'
          (while (progn
                   (forward-line 1)
                   (or (not (looking-at search-expr))
                       (eobp))))
          (when (looking-at search-expr)
            (setq arc-file (expand-file-name
                            (concat (match-string-no-properties 1) ".zip")))
            (setq path-in-arc-file (match-string-no-properties 2))
            (setq img-file (match-string-no-properties 3))
            (setq dest-dir (concat "./" (file-name-base arc-file)
                                   "_zip/" path-in-arc-file))
            (setq dest-dir-full-path (concat (file-name-sans-extension arc-file)
                                             "_zip/" path-in-arc-file))
            (setq img-file-full-path (expand-file-name img-file dest-dir))
            ;(message (concat "arc-file: %s\npath-in-arc-file: %s\n"
            ;                 "img-file: %s\nimg-file-full-path: %s\n"
            ;                  "dest-dir: %s\ndest-dir-full-path: %s")
            ;          arc-file path-in-arc-file
            ;          img-file img-file-full-path
            ;          dest-dir dest-dir-full-path)

            (when (file-newer-than-file-p arc-file img-file-full-path)
              ;; This block is executed only if arc-file is newer than
              ;; img-file-full-path
              ;; or if img-file does not exist
              ;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Testing-Accessibility.html
              (when (file-exists-p dest-dir-full-path)
                (delete-directory dest-dir-full-path t t))
              (make-directory dest-dir-full-path t)
              (setq cmd (format "unzip -j %s %s%s -d ./%s."
                                arc-file path-in-arc-file img-file
                                (concat (file-name-base arc-file) "_zip/"
                                        path-in-arc-file)))
              (message "%s" cmd)
              (shell-command cmd)
			  (setq cmd (concat "touch " img-file-full-path)) ;;touch not work in windows;
			  (message "%s" cmd)
              ;(shell-command cmd)
			  )))))))
